﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
/*
namespace Rtf
{
    public class Writer
    {
        public struct Font
        {
            public string FontName;
            public CodePage CodePage;
        }

        public enum Justify { Left, Right, Justify, Center }

        public enum CharacterFormatting { Bold, Underline, Italic, Subscript, Superscript, Smallcaps, Strikethru }

        public enum LanguageCode : ushort { None = 1024, English = 1033, French = 1036, Arabic = 3073, Greek = 1032, Hebrew = 1037 }
        public enum CodePage : ushort { None = 0, English = 1252, French = 1252, Arabic = 1256, Greek = 1253, Hebrew = 1255 }

        TextWriter _writer;

        private bool _inTable = false;

        public Writer(TextWriter writer, Font[] fonts)
        {
            _writer = writer;
            WriteProlog(fonts);
        }

        public void StartParagraph(Justify justify, int leftIndenting, int rightIndenting, int firstLineIndenting) // Twips
        {
            _writer.WriteLine(@"{{\pard\li{0}\ri{1}\fi{2}\q{3}", leftIndenting, rightIndenting, firstLineIndenting, GetJustifyCharacter(justify));
        }

        public void SetCharacterFormatting(CharacterFormatting formatting, bool isOn)
        {
            if (isOn)
                _writer.WriteLine(GetFormattingTag(formatting));
            else
                _writer.WriteLine(GetFormattingTag(formatting) + "0");
        }

        public void InsertLineBreak()
        {
            _writer.WriteLine(@"\line");
        }

        public void SetFont(int id)
        {
            _writer.WriteLine(@"\f{0}", id);
        }

        public void SetFontSize(int size) // half points
        {
            _writer.WriteLine(@"\fs{0}", size);
        }

        public void SetLanguage(LanguageCode code)
        {
            _writer.WriteLine(@"\lang{0}", (ushort)code);
        }

        public void InsertFootnote(string text)
        {
            _writer.WriteLine(@"{{\super\chftn}}{{\footnote\ftnbj\pard\plain{{\super\chftn}}. {0}}}", text);
        }

        public void InsertFootnote(string identification, string text)
        {
            _writer.WriteLine(@"{{\super {0}}}{{\footnote\ftnbj\pard\plain{{\super {0}}}. {1}}}", identification, text);
        }

        private char GetJustifyCharacter(Justify justify)
        {
            switch (justify)
            {
                case Justify.Center:
                    return 'c';
                case Justify.Justify:
                    return 'j';
                case Justify.Right:
                    return 'r';
                case Justify.Left:
                    return 'l';
                default:
                    throw new ArgumentException();
            }
        }

        private string GetFormattingTag(CharacterFormatting formatting)
        {
            switch (formatting)
            {
                case CharacterFormatting.Bold:
                    return @"\b";
                case CharacterFormatting.Underline:
                    return @"\ul";
                case CharacterFormatting.Italic:
                    return @"\i";
                case CharacterFormatting.Subscript:
                    return @"\sub";
                case CharacterFormatting.Superscript:
                    return @"\super";
                case CharacterFormatting.Smallcaps:
                    return @"\scaps";
                case CharacterFormatting.Strikethru:
                    return @"\strike";
                default:
                    throw new ArgumentException();
            }
        }

        public void EndParagraph()
        {
            _writer.WriteLine(@"\par}");
        }

        public void InsertContent(string text)
        {
            _writer.WriteLine(text.Replace("\\", "\\\\").Replace("{", "\\{").Replace("}", "\\}"));
        }

        public void InsertUnicodeCharacter(char character)
        {
            _writer.WriteLine(@"\u{0}?", (ushort)character);
        }

        void StartHeader()
{
	_writer.WriteLine("{\\*\\header ");
}
void EndHeader()
{
	_writer.WriteLine("}");
}
void StartFooter()
{
	_writer.WriteLine("{\\*\\footer ");
}
void EndFooter()
{
	_writer.WriteLine("}");
}
void AddPageNumber()
{
_writer.WriteLine("\\chpgn ");
}
void StartParagraph(RichLanguage Lang, RichJust Just)
{
	_writer.WriteLine("\\pard ");
	if(_inTable) _writer.WriteLine("\\intbl ");
	if(Lang==CRichWriter::Arabic)
		_writer.WriteLine("\\rtlpar ");
	else
		_writer.WriteLine("\\ltrpar ");
	if(Just!=NotSpec)
	{
		_writer.WriteLine("\\q");
		_writer.WriteLine(JstChar[(int)Just]);
	}
	_writer.WriteLine("\r\n");
	SetLang(Lang);
}

void SetLangDirection(bool isRTL)
{
    if (isRTL)
		_writer.WriteLine("\\rtlch ");
	else
		_writer.WriteLine("\\ltrch ");
}
void SetFont(int n)
{
	CStringA f;
	f.Format("\\f%d ",n);
	_writer.WriteLine(f);
}
void SetFontSize(int n)
{
	CStringA f;
	f.Format("\\fs%d ",n);
	_writer.WriteLine(f);
}


void AddTab()
{
	_writer.WriteLine("\\tab ");
}
void AddHardSpace()
{
	_writer.WriteLine("\\~ ");
}
void EndParagraph()
{
    if (!_inTable) _writer.WriteLine("\\par\r\n");
}
void StartTable()
{
	_writer.WriteLine("\\trowd ");
}
void StartTableDef()
{
	RowWidth = 0;
}
void DefCell(int twpWidth)
{
	CStringA f;
	f.Format("\\cellx%d ",twpWidth + RowWidth);
	_writer.WriteLine(f);
	RowWidth += twpWidth;
}
void EndTableDef()
{
	_writer.WriteLine("\n\r");
}
void StartRow()
{
}
void StartCell()
{
    _inTable = TRUE;
}
void EndCell()
{
	_writer.WriteLine("\\cell\r\n");
    _inTable = FALSE;
}
void EndRow()
{
	_writer.WriteLine("\\row\r\n");
}
void EndTable()
{
}
void EndDocument()
{
	_writer.WriteLine("}");
}

void SetPageNumber(int n)
{
	CStringA f;
	f.Format("\\pgnstarts%d ",n);
	_writer.WriteLine(f);
}
void PageAdvance()
{
	_writer.WriteLine("\\page\r\n");
	// \insrsid1 to make Word do Advance page (??!!!) (not documented)
	// \par to do advance before table (???!!!!)
}

        private void WriteProlog(Font[] fonts)
        {
            // Document Prolog
            _writer.WriteLine(@"{\rtf1\ansi\deff0{\fonttbl");
            // Fonts table
            for (int i = 0; i < fonts.Length; i++)
                _writer.WriteLine(@"{{\f{0}\cpg{1} {2};}}", i, (ushort)fonts[i].CodePage, fonts[i].FontName);
            // End Prolog
            WriteClosingBrace();

            // Document formatting
            _writer.WriteLine(@"\deflang1033\widowctrl");
            // Section formatting
            _writer.WriteLine(@"{\header\pard\qr\plain\f0\chpgn\par}"); // TODO
        }

        private void WriteClosingBrace()
        {
            _writer.WriteLine("}");
        }

        public void Close()
        {
            WriteClosingBrace();
            _writer.Close();
        }
    }
}
*/